package com.csscis.ssm.core.task;

/**
 * Created by qinghe on 2016/3/28.
 */

import com.csscis.ssm.core.color.PicAnalysis;
import com.csscis.ssm.core.util.Sender;
import com.csscis.ssm.core.util.VeDate;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ScheduleTask {

    private static final Logger logger = LoggerFactory.getLogger(ScheduleTask.class);

    @Scheduled(cron = "0 */2 * * * ?")   //每10分钟点执行一次
    public void generateAuto() throws IOException {
        String nowDate = VeDate.dateToStrLong(new Date());

        PicAnalysis picAnalysis = new PicAnalysis();
        boolean b = picAnalysis.returnState();
        logger.info("GF--------------------TEST--------------------------1" );
        try {
            this.send("13662296210,13750187371,13926618873,13927666560","TEST :"+nowDate+" 上游充水堰预警水位：25米  上游充水堰报警水位：25米","10000");
//            this.send("13662296210","TEST :"+nowDate+" 上游充水堰预警水位：25米  上游充水堰报警水位：25米","10000");
        } catch (Exception e) {
            logger.info("GF--------------------TEST--------------------------SEND FAIL" );
            e.printStackTrace();
        }
        logger.info("GF--------------------TEST--------------------------2" );
        if (b) {
            Sender.sendMsg();
            logger.info("===>warning ..." + "state:" + b + "     " + new Date());
        } else {
            logger.info("===>No warning ..." + "state:" + b + "     " + new Date());
        }
    }

    /**
     * 程序中访问http数据接口
     */
    public String send(String userNames, String message, String sendId) throws Exception {
        //StringBuffer path = new StringBuffer("http://192.168.3.12:88/flxoa/oamessage.ext?pmethod=sendMessage");
        //path.append("&receiveIds=").append(userNames).append("&content=").append(message).append("&sendId=").append(sendId);
        List params = new ArrayList();
        params.add(new BasicNameValuePair("pmethod", "sendMessage"));
        params.add(new BasicNameValuePair("receiveIds", userNames));
        params.add(new BasicNameValuePair("content", message));
        params.add(new BasicNameValuePair("sendId", sendId));
        params.add(new BasicNameValuePair("msgType", "jiankong"));
        String param = URLEncodedUtils.format(params, "UTF-8");
        URI uri = URIUtils.createURI("http", "192.168.3.12", 88, "/flxoa/oamessage.ext", param, null);
        //URI uri = URIUtils.createURI("http", "127.0.0.1", 88, "/flxoa/oamessage.ext", param, null);
        System.out.println(uri);
        String result = "";
        //1.得到浏览器
        HttpClient httpClient = new DefaultHttpClient();//浏览器
        //2指定请求方式
        //HttpGet httpGet = new HttpGet(path.toString());
        HttpGet httpGet = new HttpGet(uri);
        //3.执行请求
        HttpResponse httpResponse = httpClient.execute(httpGet);
        //4 判断请求是否成功
        int status = httpResponse.getStatusLine().getStatusCode();
        if (status == 200) {
            //读取响应内容
            result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        }
        return result;
    }
}
